package unittest;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import file.RemoteSharedFile;
import junit.framework.TestCase;

public class TestRemoteSharedFile extends TestCase {

	protected void setUp() throws Exception {
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	private RemoteSharedFile loadfile(String path, String kewWord)
	{
		BufferedReader is;
		
		try {
			is = new BufferedReader(new FileReader(path));

			String input = is.readLine();
			input += is.readLine();
			input += is.readLine();
			return new RemoteSharedFile(path, input, "", kewWord);

		} catch (FileNotFoundException e) {
			return null;
		} catch (IOException e) {
			return null;
		}
	}

	public void testKeywordMatch() {
		String path = "src/test/mock_server.py";
		RemoteSharedFile r = loadfile(path, "import");
		if(r == null)
			fail("can not load file");
		
		int n = r.keywordMatch();
		assertEquals(1, n);
	}

	public void testCompareTo() {
		RemoteSharedFile r1 = loadfile("src/test/mock_server.py", "from");

		RemoteSharedFile r2 = loadfile("src/test/ftp_client.py", "from");
		
		RemoteSharedFile r3 = loadfile("src/test/ClientTest.java", "from");
		
		List<RemoteSharedFile> l;
		
		l = new ArrayList<RemoteSharedFile>();
		l.add(r1);
		l.add(r2);
		l.add(r3);
		
		Collections.sort(l);
		
		assertEquals(r2.name(), l.get(0).name());
		assertEquals(r1.name(), l.get(1).name());
		assertEquals(r3.name(), l.get(2).name());
	}

}
